﻿//------------------------------------------------------------
// Author: 烟雨迷离半世殇
// Mail: 1778139321@qq.com
// Data: 2019年12月31日 14:23:32
//------------------------------------------------------------

using System;
using System.Collections.Generic;


namespace NKG_MVVM_UI
{
    public class UIManager : SingletonTemplate<UIManager>
    {
        public Dictionary<Type, AViewLogic> AllUIs =
            new Dictionary<Type, AViewLogic>();

        /// <summary>
        /// 获取UI
        /// </summary>
        public T GetUI<T>() where T : AViewLogic
        {
            AViewLogic tempViewLogic;
            if (AllUIs.ContainsKey(typeof(T)))
            {
                tempViewLogic = AllUIs[typeof(T)];
                return (T) tempViewLogic;
            }

            Console.WriteLine("要获取的UI不存在");
            return null;
        }

        /// <summary>
        /// 打开UI
        /// </summary>
        public T OpenUI<T>() where T : AViewLogic
        {
            AViewLogic tempViewLogic;
            if (AllUIs.ContainsKey(typeof(T)))
            {
                //TODO：支持同一种类UI的多个实例
                Console.WriteLine("已经有了同种UI，请勿重复打开！");
                return null;
            }
            else
            {
                tempViewLogic = ViewFactory.FetchView<T>();
                tempViewLogic.OnOpen();
                AllUIs.Add(typeof(T), tempViewLogic);
                return (T) tempViewLogic;
            }
        }

        /// <summary>
        /// 回收UI
        /// </summary>
        public void CloseUI(AViewLogic aViewLogic)
        {
            if (AllUIs.Remove(aViewLogic.GetType()))
            {
                aViewLogic.OnClose();
                ViewFactory.RecycleView(aViewLogic);
            }
            else
            {
                Console.WriteLine("要关闭的UI不存在");
            }

        }
    }
}